<template>
  <div id="app">
    <!-- 卡片组件 -->
    <el-card class='login-card'>
      <!-- 登录表单 -->
      <el-form
        ref="loginFormRef"
        style="margin-top: 50px"
        :model="loginForm"
        :rules="rules"
      >
        <el-form-item prop="mobiel">
          <el-input
            v-model="loginForm.mobiel"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="width: 100%"
            @click="handerForm"
          >登录</el-button>
        </el-form-item>

      </el-form>
    </el-card>
  </div>
</template>

<script>

export default {
  name: 'App',
  components: {
  },
  data () {
    const countValidatorMobile = (rule, value, callback) => {
      value[2] === '9' ? callback() : callback(new Error('第三个数字必须为9'))
    }
    return {
      loginForm: {
        mobiel: "",
        password: ""
      },
      rules: {
        mobiel: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' },
          { validator: countValidatorMobile, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码应为6-16位的长度', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handerForm () {
      try {
        await this.$refs['loginFormRef'].validate()
        console.log('通过验证,登录成功')
      } catch (err) {
        console.log(err.message);
      }
    }
  }
}
</script>

<style>
#app {
  width: 100%;
  height: 100vh;
  background-color: pink;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-card {
  width: 440px;
  height: 300px;
}
</style>